//
//  CustomPickView.m
//  jingjing2.0
//
//  Created by apple on 16/5/14.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "CustomPickView.h"

@interface CustomPickView () <UIPickerViewDataSource, UIPickerViewDelegate>
{
    UIToolbar   *_toolBar;
    CGSize      _size;
}
@end
@implementation CustomPickView

- (id)initWithIsTime:(BOOL)isTime
{
    if (self = [super init]) {
        self.alpha = 0;
        _isTime = isTime;
        _size = [[UIScreen mainScreen] bounds].size;
        self.backgroundColor = [UIColor clearColor];
        [self addsubViews];
    } return self;
}
-(void)setDataArray:(NSArray *)dataArray
{
    _dataArray = dataArray;
    [_pickView reloadAllComponents];
}
-(void)setDateArray:(NSMutableArray *)dateArray
{
    _dateArray=dateArray;
    [_pickView reloadAllComponents];

}
- (void)addsubViews
{
    self.frame = (CGRect){CGPointZero, _size};
    [[[[UIApplication sharedApplication] delegate] window] addSubview:self];
    
    _toolBar = [[UIToolbar alloc] init];
    _toolBar.tintColor = BLUECOLOR;
    _toolBar.frame = (CGRect){0, _size.height, _size.width, 44};
    [self addSubview:_toolBar];
    
    // 按钮自定义
    UIBarButtonItem *item1=[[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(dismiss)];
   
    UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *item3=[[UIBarButtonItem alloc]initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(done)];
    
    _toolBar.items = [NSArray arrayWithObjects:item1, item2, item3, nil];
    
    self.pickView = [[UIPickerView alloc] init];
    self.pickView.showsSelectionIndicator = YES;
    self.pickView.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1];
    self.pickView.dataSource = self;
    self.pickView.delegate = self;
    self.pickView.frame = (CGRect){0, _size.height + 44, _size.width, 206 - 44};
    [self addSubview:self.pickView];
}

+ (void)pickViewWithArray:(NSArray *)array forUILabel:(UILabel *)label
{
    CustomPickView *pickView = [[self alloc] initWithArray:array];
    pickView.label = label;
    [pickView show];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    if (!_isTime) {
         return 1;
    }else
    {
        return 2;
    }
   
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (_isTime) {
        if (component == 0)
        {
            [pickerView reloadComponent:1];
        }

    }
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (!_isTime) {
        return _dataArray.count;
    }else
    {
        if (component == 0)
        {
            return _dataArray.count;
        }else
        {
            NSInteger row = [pickerView selectedRowInComponent:0];
                NSArray *arr=[_dateArray objectAtIndex:row];
            return arr.count;
            return 0;
        }
   }
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (!_isTime)
    {
        NSDictionary *dic=_dataArray[row];
        return [NSString stringWithFormat:@"%@",dic[@"CollTypeName"]];
    }else
    {
        if (component == 0)
        {
            NSDictionary *dic=[_dataArray objectAtIndex:row];
           return [NSString stringWithFormat:@"%@(%@)",dic[@"CollDate"],dic[@"DayName"]];;
        }else
        {
                NSInteger selectRow = [pickerView selectedRowInComponent:0];
                NSArray *arr=[_dateArray objectAtIndex:selectRow];
                NSDictionary *dicTime=[arr objectAtIndex:row];
                return [NSString stringWithFormat:@"%@",dicTime[@"TimeGroupName"]];
            return nil;
      }
    
   }
}

- (void)show
{
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 1;
        _toolBar.frame = (CGRect){0, _size.height - 206, _size.width, 44};
        self.pickView.frame = (CGRect){0, _size.height - 206 + 44, _size.width, 206 - 44};
    }];
}

- (void)dismiss
{
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0;
        _toolBar.frame = (CGRect){0, _size.height, _size.width, 44};
        self.pickView.frame = (CGRect){0, _size.height + 44, _size.width, 206 - 44};
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)done
{
    if (_isTime)
    {
        NSDictionary *dicDate=[_dataArray objectAtIndex:[self.pickView selectedRowInComponent:0]];
        NSArray *arr=[_dateArray objectAtIndex:[self.pickView selectedRowInComponent:0]];
        NSDictionary *dicTime=[arr objectAtIndex:[self.pickView selectedRowInComponent:1]];
        self.label.text = [NSString stringWithFormat:@"%@ (%@)%@",dicDate[@"CollDate"],dicDate[@"DayName"],dicTime[@"TimeGroupName"]];
    }else
    {
        NSDictionary *dic=[_dataArray objectAtIndex:[self.pickView selectedRowInComponent:0]];
        
      self.label.text = dic[@"CollTypeName"];
    }
    [self dismiss];
}

@end
